package edu.sctu.xialei.june.day20150619one;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;

/**
 * Created by Administrator on 2015/6/18.
 */
public class UrlOperation {
    private URL url;
    private String path = "http://localhost:8080/xialei/";
    private Scanner in = new Scanner(System.in);

    public void setUrlPOST() throws IOException {
        String cPath = path + in.next();

        url = new URL(cPath);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

//        httpURLConnection.connect();

        httpURLConnection.setDoOutput(true);
        httpURLConnection.setDoInput(true);

        OutputStream outputStream = httpURLConnection.getOutputStream();
        outputStream.write("one=100".getBytes());

        InputStream inputStream = httpURLConnection.getInputStream();
        byte value[] = new byte[10240];
        inputStream.read(value);

        System.out.println(new String(value));
    }

    public void setUrlGET() throws IOException {
        String cPath = path + in.next();

        url = new URL(cPath);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

        httpURLConnection.connect();

        InputStream inputStream = httpURLConnection.getInputStream();
        byte value[] = new byte[10240];
        inputStream.read(value);
        System.out.println(new String(value));
    }

}
